package com.ebupt.migu.restapi;

import cn.hutool.http.HttpUtil;
import com.ebupt.migu.common.util.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @author tz
 */
@Slf4j
public class EsRestHttp {

    private static PropertiesUtil prop = new PropertiesUtil();
    private static String esUrl = prop.getValue("esUrl");
    private static String esUser = prop.getValue("esUser");
    private static String esPwd = prop.getValue("esPwd");


    public String search(String index,String key,String value,int size){
        return search(index, key + ":" + value,size);
    }

    public String search(String index,String param,int size){
        if (StringUtils.isEmpty(index)){
            log.info("Index is empty!");
            return "";
        }
        String url = esUrl + "/" + index + "/_search?q=" + param;
        if (size > 0){
            url = url + "&size=" + size;
        }
        log.info("URL：{}", url);
        return HttpUtil.createGet(url).basicAuth(esUser, esPwd).execute().body();
    }

}
